package view.manager;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.CardLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import base.NumberGeneration;
import dao.DepartmentDao;
import dao.MajorDao;
import model.Department;
import model.Major;
import util.StringUtil;

import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class MajorAddInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTextField textField_1;
	private JComboBox dpartmentType;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MajorAddInterfrm frame = new MajorAddInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MajorAddInterfrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("系部添加");
		setBounds(100, 100, 450, 323);
		
		JLabel lblNewLabel = new JLabel("    系部编号：");
		lblNewLabel.setIcon(new ImageIcon(MajorAddInterfrm.class.getResource("/images/num.png")));
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("    系部名称：");
		lblNewLabel_1.setIcon(new ImageIcon(MajorAddInterfrm.class.getResource("/images/name.png")));
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("上级学院编号：");
		lblNewLabel_2.setIcon(new ImageIcon(MajorAddInterfrm.class.getResource("/images/num.png")));
		
		JLabel lblNewLabel_3 = new JLabel("    系部描述：");
		lblNewLabel_3.setIcon(new ImageIcon(MajorAddInterfrm.class.getResource("/images/describe.png")));
		
		JTextArea textArea = new JTextArea();
		textArea.setBorder(new LineBorder(new Color(127,157,185), 1, false));
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField.getText())) {
					JOptionPane.showMessageDialog(null, "请先选择学院", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					return;
				}
//				if (!StringUtil.isNum(textField.getText())) {
//					JOptionPane.showMessageDialog(null, "系部编号需为数字", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					return;
//				}
//				if (textField.getText().length() >= 9) {
//					JOptionPane.showMessageDialog(null, "系部编号太长", "提示", JOptionPane.ERROR_MESSAGE);
//					textField.requestFocus();
//					textField.selectAll();
//					return;
//				}
				
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "系部名称不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					return;
				}
				if (textField_1.getText().length() >= 30) {
					JOptionPane.showMessageDialog(null, "系部名称太长", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					textField_1.selectAll();
					return;
				}
				if (textArea.getText().length() >= 50) {
					JOptionPane.showMessageDialog(null, "系部描述太长", "提示", JOptionPane.ERROR_MESSAGE);
					textArea.requestFocus();
					textArea.selectAll();
					return;
				}
				ArrayList<Major> arrayList = new ArrayList<Major>();
				arrayList = MajorDao.select();
				for (Major major : arrayList) {
					if (textField_1.getText().equals(major.getM_name())) {
						JOptionPane.showMessageDialog(null, "系部名称已存在", "提示", JOptionPane.ERROR_MESSAGE);
						textField_1.requestFocus();
						textField_1.selectAll();
						return;
					}
				}
				Major major = new Major();
				major.setM_id(textField.getText());
				Department department = (Department)dpartmentType.getSelectedItem();
				major.setD_id(department.getD_id());
				major.setM_name(textField_1.getText());
				major.setRemarks(textArea.getText());
				if (MajorDao.add(major)>0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					dpartmentType.setSelectedIndex(0);
					if (dpartmentType.getSelectedItem() instanceof Department) {
						textField.setText(NumberGeneration.createMajorNum((Department)dpartmentType.getSelectedItem()));
					}else {
						textField.setText("");
					}
					textField_1.setText("");
					textArea.setText("");
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(MajorAddInterfrm.class.getResource("/images/add.png")));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dpartmentType.setSelectedIndex(0);
				textField.setText("");
				textField_1.setText("");
				textArea.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(MajorAddInterfrm.class.getResource("/images/resets.png")));
		
		dpartmentType = new JComboBox();
		dpartmentType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dpartmentType.getSelectedItem() instanceof Department) {
					textField.setText(NumberGeneration.createMajorNum((Department)dpartmentType.getSelectedItem()));
				}else {
					textField.setText("");
				}
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(98)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel_3))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(textField, Alignment.LEADING)
										.addComponent(dpartmentType, Alignment.LEADING, 0, 148, Short.MAX_VALUE))
									.addContainerGap())
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(textField_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
										.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
									.addGap(74))))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addGap(64)
							.addComponent(btnNewButton_1)
							.addContainerGap(110, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(dpartmentType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_3))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		fillDepartmentType();
	}
	
	/**
	 * 初始化下拉框
	 */
	private void fillDepartmentType() {
		ArrayList<Department> arrayList = DepartmentDao.select();
		dpartmentType.addItem("请选择上级学院");
		for (Department department : arrayList) {
			dpartmentType.addItem(department);
		}
	}
}
